package com.ziyun.erp.web.modules.logistics.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.ziyun.erp.common.e.logistics.LogisticFinanceStatusEnum;
import com.ziyun.erp.common.e.logistics.LogisticsSendTypeEnum;
import com.ziyun.erp.common.utils.ExcelUtils;
import com.ziyun.erp.common.utils.JsonUtils;
import com.ziyun.erp.common.utils.UserUtils;
import com.ziyun.erp.modules.logistics.dto.ErpLogisticsDto;
import com.ziyun.erp.modules.logistics.dto.LogisticsPayDto;
import com.ziyun.erp.modules.logistics.entity.ErpLogisticsEntity;
import com.ziyun.erp.modules.logistics.entity.LogisticsTypeEntity;
import com.ziyun.erp.modules.logistics.service.ErpLogisticsService;
import com.ziyun.erp.modules.logistics.service.LogisticsTypeService;
import com.ziyun.erp.modules.logistics.vo.CostFormVo;
import com.ziyun.erp.modules.supp.entity.SellerInfoEntity;
import com.ziyun.erp.modules.supp.service.SellerInfoService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.sys.entity.SysUserEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.File;
import java.util.*;


/**
 * 物流配送表
 *
 * @author guizhao
 * @email ...
 * @date 2018-08-31 10:12:35
 */
@RestController
@RequestMapping("logistics/erplogistics")
public class ErpLogisticsController {

    @Autowired
    private ErpLogisticsService erpLogisticsService;
    @Autowired
    private SellerInfoService sellerInfoService;
    @Autowired
    private LogisticsTypeService logisticsTypeService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = erpLogisticsService.queryPage(params);

        return R.ok().put("page", page);
    }
    /**
     * 枚举
     */
    @RequestMapping("/enumlist")
    public R enumlist(){
        Map<String, Object> map = new HashMap<>();
        List<Map<String, Object>> logisticsSendTypeEnumList = JsonUtils.enumToList(LogisticsSendTypeEnum.class);
        map.put("logisticsSendTypeEnumList",logisticsSendTypeEnumList);

        List<Map<String, Object>> logisticFinanceStatusList = JsonUtils.enumToList(LogisticFinanceStatusEnum.class);
        map.put("logisticFinanceStatusList",logisticFinanceStatusList);

        List<Integer> ids = new ArrayList<>();
        ids.add(18);
        ids.add(20);
        List<SellerInfoEntity> merchantList = sellerInfoService.selectList(new EntityWrapper<SellerInfoEntity>()
                .eq("self" ,1)
                .notIn("seller_id", ids)
        );
        map.put("merchantList",merchantList);
        //查询物流名称
        List<LogisticsTypeEntity> logisticsTypeEntityList=logisticsTypeService.selectList(new EntityWrapper<LogisticsTypeEntity>().eq("`delete`",0));
        map.put("logisticsTypeEntityList",logisticsTypeEntityList);
        return R.ok().put("map", map);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{logiId}")
    @RequiresPermissions("logistics:erplogistics:info")
    public R info(@PathVariable("logiId") Integer logiId){
        ErpLogisticsEntity erpLogistics = erpLogisticsService.selectById(logiId);

        return R.ok().put("erpLogistics", erpLogistics);
    }

    /**
     * 批量审核
     * @return
     */
    @RequestMapping("/batchAudit")
    public R batchAudit(@RequestBody Integer[] logiIds){
        Long userId = UserUtils.getCurentUser().getUserId();
        return erpLogisticsService.batchAudit(logiIds, userId);
    }

    /**
     * 保存 订单物流信息
     */
    @RequestMapping("/saveOrderLogisticsInfo")
    public R saveOrderLogisticsInfo(@Valid @RequestBody  ErpLogisticsDto erpLogisticsDto){
        SysUserEntity curentUser = UserUtils.getCurentUser();
        String userId = curentUser.getUserId().toString();
        String createName = curentUser.getUsername();
        return erpLogisticsService.saveOrderLogisticsInfo(erpLogisticsDto, userId, createName);
    }

    /**
     * 保存 订单物流信息(阳逻)
     */
    @RequestMapping("/saveYangluoLogisticsInfo")
    public R saveYangluoLogisticsInfo(@Valid @RequestBody  ErpLogisticsDto erpLogisticsDto){
        SysUserEntity curentUser = UserUtils.getCurentUser();
        String userId = curentUser.getUserId().toString();
        String createName = curentUser.getUsername();
        return erpLogisticsService.saveYangluoLogisticsInfo(erpLogisticsDto, userId, createName);
    }

    /**
     * 批量确认付款
     */
    @RequestMapping("/batchPayConfirm")
    public R batchPayConfirm(@RequestBody LogisticsPayDto logisticsPayDto){
        logisticsPayDto.setUserId(UserUtils.getCurentUser().getUserId());
        return erpLogisticsService.batchPayConfim(logisticsPayDto);
    }

    /**
     * 弃审
     */
    @RequestMapping("/back")
    public R back(@RequestParam("logiId") Integer logiId){
        return erpLogisticsService.back(logiId);
    }

    /**
     * 录入成本
     */
    @RequestMapping("/enteringCost")
    public R enteringCost(@RequestBody CostFormVo costFormVo){
        return erpLogisticsService.enteringCost(costFormVo);
    }

    /**
     * 物流信息查询
     */
    @RequestMapping("/searchLogisticsInfoApi")
    public R searchLogisticsInfoApi(@RequestParam("logiId") Integer logiId, @RequestParam("logiNo") String logiNo){
        return erpLogisticsService.searchLogisticsInfoApi(logiId, logiNo);
    }

    /**
     * 修改物流单号
     */
    @RequestMapping("/updateLogisticsInfo")
    public R updateLogisticsInfo(@RequestParam("logiId") Integer logiId, @RequestParam("logiNo") String logiNo){
        return erpLogisticsService.updateLogisticsInfo(logiId, logiNo);
    }

    /**
     * 物流批量导入
     */
    @RequestMapping("/exportLogistics")
    public R exportLogistics(){
        // 读取文件
        File file = new File("C:\\Users\\Administrator\\Desktop\\物流发货导入0.xls");
        List<Map<String, Object>> mapData = ExcelUtils.importExcelByMap(file);

        String msg = "";
        if(mapData != null && mapData.size() > 0){
            msg = erpLogisticsService.exportLogistics(mapData);
        }
        return R.ok(msg);
    }

    /**
     * 物流批量导入
     */
    @RequiresPermissions("logistics:erplogistics:importLogistics")
    @RequestMapping("/importLogistics")
    public R importLogistics(@RequestParam("file") MultipartFile multipartFile){
        SysUserEntity curentUser = UserUtils.getCurentUser();
        Long userId = curentUser.getUserId();
        String username = curentUser.getUsername();
        return erpLogisticsService.importLogistics(multipartFile, userId, username);
    }

    /**
     * 物流删除
     */
    @RequestMapping("/deleteLogi")
    public R deleteLogi(@RequestParam("logiId") Integer logiId){
        return erpLogisticsService.deleteLogi(logiId);
    }

    /**
     * 物流金额计算
     */
    @RequestMapping("/logiCostCount")
    public R logiCostCount(@RequestBody Integer[] logiIds){
        return erpLogisticsService.logiCostCount(logiIds);
    }

}
